﻿Public Class FloFauSit

#Region "Declaration"
    Dim db As New Connexion
    Dim idEsp, idDescription As Integer
    Dim TextBoxNiv As String
    Dim TextBoxCodGenr As String
    Dim TextBoxCodFam As String
    Dim TextBoxCodOrd As String
    Dim TextBoxCodEsp As String
    Dim TextBoxCodSSEsp, TextBoxCodTax As String
    Public databaseName As String
    Dim pas, Action As String
    Dim Niveau As Int16
    Public OrdreInsertion As String
    Dim contextMenu1 As New ContextMenu
    Dim menuItem1 As New MenuItem("&Avant")
    Dim menuItem2 As New MenuItem("&Aprés")
    Dim menuItem3 As New MenuItem("&Annuler")
    Dim point1 As New System.Drawing.Point

    Public WithEvents WB_NAV As New WebBrowser
    Dim Entrer As String

    Dim LibelleNiv As String()
    Dim CodeNiv As String()
    Dim NomNiv As String()
    Dim IdNiv As String()
    Dim Nomtable As String()
    Dim AutresLang As String()
    Dim NbrNiv As Integer = 0
#End Region

    Private Sub FloFauSit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        NbrNiv = 8
        LibelleNiv = New String(NbrNiv) {}
        LibelleNiv = {"Regne", "Phylum", "Classe", "Ordre", "Famille", "Genre", "Espece", "Sous Espece"}
        CodeNiv = New String(NbrNiv) {}
        NomNiv = New String(NbrNiv) {}
        IdNiv = New String(NbrNiv) {}
        AutresLang = New String(4) {}
        Nomtable = New String(19) {}

        Dim iter As Integer = 0
        db.myReq = "SELECT syscolumns.name AS colonne FROM sysobjects , syscolumns WHERE  ( sysobjects.id = syscolumns.id) And  (sysobjects.xtype = 'U') and sysobjects.name= 'FloFauSyst'"
        db.Connecter(databaseName)
        iter = 0
        While db.rdr.Read()
            Nomtable(iter) = db.rdr(0)
            iter = iter + 1
        End While
        db.Disconnecter()

        Niveau = 1
        Entrer = ""

        miseAjour()
    End Sub
    Private Sub miseAjour()

        Dim requeteX As String
        Dim niv As Integer = 1


        requeteX = "Select * from  [dbo].[FloFauSyst] WHERE " & Nomtable(5 + NbrNiv) & " = '" & Niveau & "' "

        While niv < Niveau
            requeteX = requeteX & "and " & Nomtable(4 + niv) & " = '" & CodeNiv(niv - 1) & "' "
            niv = niv + 1
        End While

        
        If Niveau = 1 Then
            Me.ButtonBack.Enabled = False
            Me.ButtonNext.Enabled = True
        ElseIf Niveau = NbrNiv Then
            Me.ButtonBack.Enabled = True
            Me.ButtonNext.Enabled = False
        Else
            Me.ButtonBack.Enabled = True
            Me.ButtonNext.Enabled = True
        End If

        db.myReq = requeteX
        db.Connecter(databaseName)
        Me.ComboBoxIdEsp.Items.Clear()
        Me.ComboBoxNomType.Items.Clear()
        While db.rdr.Read()
            Dim iterCode As Integer = 0
            Dim result As String = ""
            Me.ComboBoxIdEsp.Items.Add(db.rdr(0))
            Me.ComboBoxNomType.Items.Add(db.rdr(14))
        End While
        db.Disconnecter()

    End Sub

    Private Sub ButtonNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNext.Click
        Entrer = ""

        Niveau = Niveau + 1
        Me.Label7.Text = LibelleNiv(Niveau)

        db.myReq = "Select * from  [dbo].[FLOFAUSYST] WHERE " & Nomtable(0) & " = '" & Me.ComboBoxIdEsp.Text & "'"
        db.Connecter(databaseName)
        While db.rdr.Read
            CodeNiv(Niveau - 2) = db.rdr(2 + Niveau)
        End While

        miseAjour()
    End Sub

    Private Sub ButtonBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonBack.Click
        Dim niv As Integer = 0
        Me.TextBoxDecoupageEsp.Text = ""
        Entrer = ""
        Niveau = Niveau - 1
        Me.Label7.Text = LibelleNiv(Niveau - 1)
        While niv < Niveau
            Me.TextBoxDecoupageEsp.Text = Me.TextBoxDecoupageEsp.Text & NomNiv(niv)
            niv = niv + 1
        End While
        miseAjour()
        Me.ComboBoxNomType.SelectedIndex = IdNiv(Niveau - 1)
    End Sub

    Private Sub ComboBoxNomType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxNomType.SelectedIndexChanged


        Dim niv As Integer = 0
        Dim IdRar, IdEnd As Integer
        Me.TextBoxDecoupageEsp.Text = ""

        db.myReq = "Select * from  [dbo].[FLOFAUSYST] WHERE " & Nomtable(0) & " = '" & Me.ComboBoxIdEsp.Text & "'"
        db.Connecter(databaseName)
        While db.rdr.Read
            IdRar = db.rdr(1)
            IdEnd = db.rdr(2)
            NomNiv(Niveau - 1) = db.rdr(14)
        End While
        db.Disconnecter()

        db.myReq = "Select [Nom_RarTyp] from  [dbo].[RarStatTyp] WHERE [ID_RarTyp] = '" & IdRar & "'"
        db.Connecter(databaseName)
        While db.rdr.Read
            Me.TextBoxRar.Text = db.rdr(0)
        End While
        db.Disconnecter()

        db.myReq = "Select [Nom_EndTyp] from  [dbo].[ENDSTATTYP] WHERE [Id_EndTyp] = '" & IdEnd & "'"
        db.Connecter(databaseName)
        While db.rdr.Read
            Me.TextBoxRar.Text = db.rdr(0)
        End While
        db.Disconnecter()

        While niv < NbrNiv
            Me.TextBoxDecoupageEsp.Text = Me.TextBoxDecoupageEsp.Text & NomNiv(niv)
            niv = niv + 1
        End While



        Dim Pos As Int32 = Me.ComboBoxNomType.Text.Length
        Me.ComboBoxNomType.SelectedIndex = Me.ComboBoxNomType.FindString(Me.ComboBoxNomType.Text)
        Me.ComboBoxNomType.SelectionStart = Pos
        Me.ComboBoxNomType.SelectionLength = Me.ComboBoxNomType.Text.Length - Pos
        Me.ComboBoxIdEsp.SelectedIndex = Me.ComboBoxNomType.FindString(Me.ComboBoxNomType.Text)


        IdNiv(Niveau - 1) = Me.ComboBoxNomType.SelectedIndex

    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Dim idEco, idPatr As Integer
        db.myReq = " INSERT INTO  [dbo].[FLOFAUECOL2]([FREQ_ECO]) VALUES('" & Me.TextBoxEco.Text & "')"
        db.Connecter(databaseName)
        db.Disconnecter()
        db.myReq = "Select max([ID_Eco]) FROM  [dbo].[FLOFAUECOL2]"
        db.Connecter(databaseName)
        While db.rdr.Read()
            idEco = db.rdr(0)
        End While
        db.Disconnecter()

        db.myReq = " INSERT INTO  [dbo].[FloFauPatrim2]([Expl_FloFauPatr],[Cons_FloFauPatr],[Act_FloFauPatr],[PrvFloFauPatr]) VALUES('" & Me.TextBoxDesc1.Text & "','" & Me.TextBoxDesc2.Text & "','" & Me.TextBoxDesc3.Text & "','" & Me.TextBoxDesc4.Text & "')"
        db.Connecter(databaseName)
        db.Disconnecter()
        db.myReq = "Select max([Id_FloFauPatrim]) FROM  [dbo].[FloFauPatrim2]"
        db.Connecter(databaseName)
        While db.rdr.Read()
            idPatr = db.rdr(0)
        End While
        db.Disconnecter()


        db.myReq = " INSERT INTO  [dbo].[FLOFAUSIT2]([Id_FloFauSyst],[ID_Eco],[Id_FloFauPatrim],[CODE_FFS],[ABOND_FFS],[EXPLVAL_FFS]) VALUES('" & Me.ComboBoxIdEsp.Text & "','" & idEco & "','" & idPatr & "','','" & Me.TextBoxAbdc.Text & "','" & Me.TextBoxExp.Text & "')"
        db.Connecter(databaseName)
        db.Disconnecter()

    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        Dim intEsp As New FloFauNouv(databaseName)
        intEsp.Visible = True
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Me.Close()
    End Sub



End Class